package com.philips.work.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.philips.common.core.utils.StringUtils;
import com.philips.work.domain.YwAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.philips.common.log.annotation.Log;
import com.philips.common.log.enums.BusinessType;
import com.philips.common.security.annotation.RequiresPermissions;
import com.philips.work.domain.YwUnit;
import com.philips.work.service.IYwUnitService;
import com.philips.common.core.web.controller.BaseController;
import com.philips.common.core.web.domain.AjaxResult;
import com.philips.common.core.utils.poi.ExcelUtil;
import com.philips.common.core.web.page.TableDataInfo;

/**
 * 机构单位Controller
 * 
 * @author philips
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/unit")
public class YwUnitController extends BaseController
{
    @Autowired
    private IYwUnitService ywUnitService;

    /**
     * 查询机构单位列表
     */
    @RequiresPermissions("work:unit:list")
    @GetMapping("/list")
    public TableDataInfo list(YwUnit ywUnit)
    {
        startPage();
        List<YwUnit> list = ywUnitService.selectYwUnitList(ywUnit);
        return getDataTable(list);
    }

    /**
     * 查询所有机构单位列表
     */
    @GetMapping("/findAllUnitList")
    public AjaxResult findAllUnitList()
    {
        List<YwUnit> list = ywUnitService.findAllUnitList();
        if (StringUtils.isNull(list))
        {
            list = new ArrayList<YwUnit>();
        }
        return success(list);
    }

    /**
     * 导出机构单位列表
     */
    @RequiresPermissions("work:unit:export")
    @Log(title = "机构单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YwUnit ywUnit)
    {
        List<YwUnit> list = ywUnitService.selectYwUnitList(ywUnit);
        ExcelUtil<YwUnit> util = new ExcelUtil<YwUnit>(YwUnit.class);
        util.exportExcel(response, list, "机构单位数据");
    }

    /**
     * 获取机构单位详细信息
     */
    @RequiresPermissions("work:unit:query")
    @GetMapping(value = "/{ids}")
    public AjaxResult getInfo(@PathVariable("ids") Long ids)
    {
        return success(ywUnitService.selectYwUnitByIds(ids));
    }

    /**
     * 新增机构单位
     */
    @RequiresPermissions("work:unit:add")
    @Log(title = "机构单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YwUnit ywUnit)
    {
        return toAjax(ywUnitService.insertYwUnit(ywUnit));
    }

    /**
     * 修改机构单位
     */
    @RequiresPermissions("work:unit:edit")
    @Log(title = "机构单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YwUnit ywUnit)
    {
        return toAjax(ywUnitService.updateYwUnit(ywUnit));
    }

    /**
     * 删除机构单位
     */
    @RequiresPermissions("work:unit:remove")
    @Log(title = "机构单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{idss}")
    public AjaxResult remove(@PathVariable Long[] idss)
    {
        return toAjax(ywUnitService.deleteYwUnitByIdss(idss));
    }
}
